package team.group.exam.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.xwork2.ActionContext;

import team.group.exam.biz.IPaperBiz;
import team.group.exam.biz.ITitlesBiz;
import team.group.exam.dao.IPaperDao;
import team.group.exam.dao.ITitlesDao;
import team.group.exam.entity.PaperCondition;
import team.group.exam.entity.TbPaper;
import team.group.exam.entity.TbPaperBank;
import team.group.exam.entity.TbQuestionbank;

public class PaperBizImpl implements IPaperBiz {
    private IPaperDao paperDao;
    private ITitlesDao titleDao;
    public IPaperDao getPaperDao() {
		return paperDao;
	}

	public void setPaperDao(IPaperDao paperDao) {
		this.paperDao = paperDao;
	}
    
	
	public ITitlesDao getTitleDao() {
		return titleDao;
	}

	public void setTitleDao(ITitlesDao titleDao) {
		this.titleDao = titleDao;
	}
    
	/**
	 * 实现自动组卷
	 */
	@Override
    public List addRandomPaper(int singleSize,TbPaper paper) {
		/**
		 * 在此处获取到试卷id
		 */
		TbPaper papernew=paperDao.getByPaper(paper);
		// TODO Auto-generated method stub
		//获取所有的的题目
		 List allTitle=this.getAllTitle();
	    //是否满足组卷的数量
		if (singleSize > allTitle.size()) {
			return null;
		}
		//初始化数组，保存题目编号
		List titleNoList=new ArrayList();
		int maxSize= titleDao.getMaxNo();
		int index = 1;
		//初始化一个数组
		boolean flags[] = new boolean[maxSize];
		 while(index<=singleSize){
			Random random = new Random();
			int choose;
			//生成随机数据
			choose = random.nextInt(maxSize);
			//若返会true，存在
		  if(titleDao.getByID(choose)){	
			 if(!flags[choose]){
				titleNoList.add(choose);
				index +=1;
				/**
				 * save到关系表
				 */
				TbPaperBank paperBank=new TbPaperBank();
				//修改
				paperBank.setTbPaper(papernew);
				paperBank.setTbQuestionbank(titleDao.getTitleById(choose));
				paperDao.addPaperTitle(paperBank);
			 }
			 flags[choose]=true;
			}
	        }
		 
		 System.out.println(titleNoList.size());
		 return titleNoList;
	}
	@Override
	public void addPaper(TbPaper paper) {
		// TODO Auto-generated method stub
		paperDao.addPaper(paper);
	}
	

	@Override
	public List<TbPaper> getPaperByType(String type) {
		// TODO Auto-generated method stub
		return paperDao.getPaperByType(type);
	}

	
	public List<TbQuestionbank> getAllTitle(){
		
		return titleDao.getAllTitles();
	}

	@Override
	public void deletePaper(int id) {
		// TODO Auto-generated method stub
		TbPaper paper=this.getById(id);
		paperDao.deletePaper(paper);
	}
	
    /**
     * 获取试卷，通过题库试卷表
     */
	@Override
	public List<TbQuestionbank> getPaper() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TbPaper getById(int id) {
		// TODO Auto-generated method stub
		return paperDao.getById(id);
	}

	@Override
	public void addPaperTitle(TbPaperBank paperBank) {
		// TODO Auto-generated method stub
		 
		 paperDao.addPaperTitle(paperBank);
	}

	@Override
	public List<TbPaperBank> getTbPaperBankList(TbPaper paper,int currentPage,int pageSize) {
		// TODO Auto-generated method stub
		return paperDao.getTbPaperBankList(paper,currentPage,pageSize);
	}

	@Override
	public List<TbPaper> getPaperName(PaperCondition paperCondition) {
		// TODO Auto-generated method stub
		return paperDao.getPaperName(paperCondition);
	}

	@Override
	public int getTotalPage(TbPaper paper,int pageSize) {
		// TODO Auto-generated method stub
		return paperDao.getTotalPage(paper,pageSize);
	}

	@Override
	public List<TbPaperBank> getAllTitleNopage(TbPaper paper) {
		// TODO Auto-generated method stub
		return paperDao.getAllTiltle(paper);
	}
    /**
     * 得到正确答案的列表
     */
	@Override
	public List<String> getAllRightAnswer(TbPaper paper) {
		// TODO Auto-generated method stub
	     List list=paperDao.getAllRightAnswer(paper);
	     List list1=new ArrayList();
	     TbQuestionbank questionBank=null;
	     for(int i=0;i<list.size();i++){
	    	 int id=Integer.parseInt(list.get(i).toString());
	    	 System.out.println(id);
	    	 //id 是 数据库里存储的题目编号  questiongbank是正确答案 A B C D
	    	 questionBank=titleDao.getTitleById(id);
			 list1.add(questionBank.getQuestionRightAnswer());
	     }
	    
	     return list1;
	     
	}

	public Map<String,String> getRightAnswerMap(TbPaper paper)
	{
		 List list=paperDao.getAllRightAnswer(paper);
	     List list1=new ArrayList();
	     TbQuestionbank questionBank=null;
	     
	     Map<String,String> maps=new HashMap<String, String>();
	     
	     for(int i=0;i<list.size();i++){
	    	 int id=Integer.parseInt(list.get(i).toString());
	    	 questionBank=titleDao.getTitleById(id);
	    	 String answer=questionBank.getQuestionRightAnswer();
	    	 maps.put(id+"", answer);
	     }
	     
	     return maps;
	}
	/**
	 * 
	 * @param paper
	 * @param request
	 * @param state 如果为0则是执行完题目 如果为1 则是没有执行完题目
	 * @return
	 */
	public int getScore(TbPaper paper,HttpServletRequest request,int state)
	{
		if(state==0)
		{
			return this.getScore(paper, request);
		}
		else
		{
			int thescore=0;
			Map<String,String> rightmap=this.getRightAnswerMap(paper);
			Map<String, String[]> params = request.getParameterMap();
			
			for(String param:params.keySet()){
				
				String answer=params.get(param)[0];
				
				for(String right:rightmap.keySet())
				{
					if(param.equals(right))
					{
						if(answer.equals(rightmap.get(right)))
						{
							thescore+=10;
						}
					}
				}
				
			}
			return thescore;
		}
	}
	
	@Override
	public int getScore(TbPaper paper,HttpServletRequest request) {
		// TODO Auto-generated method stub

		// 正确答案列表
		List answerList = this.getAllRightAnswer(paper);
		//用户答案
		Map<String, String[]> params = request.getParameterMap();
		
		List arr = new ArrayList();
		
		//获得输入值
		for (String key : params.keySet()) {
			System.out.println("key:" + key + "    value:" + params.get(key)[0]);
			
			arr.add(params.get(key)[0]);
		}
		
		
		
		int score = 0;
		for (int i = 0; i < answerList.size(); i++) {
			if (answerList.get(i).equals(arr.get(i))) {
				score += 10;
			}
		}
		
		System.out.println("score is :"+score);
		return score;
	}

	
	/**
	 * 获得错题数据数组
	 * @param paper 试卷
	 * @param request HttpServletRequest 获取request
	 * @return
	 */
	public List getErrorQuestSet(TbPaper paper,HttpServletRequest request)
	{
		//用于保存错误题号
		ArrayList<String> err=new ArrayList<String>();
		
		//所有正确答案
		List answerList = this.getAllRightAnswer(paper);
		
		
		//用户答案
		Map<String, String[]> params = request.getParameterMap();
		
		ArrayList<String> arr = new ArrayList();
		
		List helpArr= new ArrayList<String>();
		
		//取得用户所有已答题 题号
		for (String key : params.keySet()) {
			System.out.println("key:" + key + "    value:" + params.get(key)[0]);
			helpArr.add(key);
			arr.add(params.get(key)[0]);
		}
		
		//得到所有正确答案题号
		List<Integer> theRightList= paperDao.getAllRightAnswer(paper);
		
		boolean judge[]=new boolean[theRightList.size()];
		
		List<TbQuestionbank> allQuestionList=new ArrayList<TbQuestionbank>();
		
		for(Integer answernumber:theRightList)
		{
			allQuestionList.add(titleDao.getTitleById(answernumber));
		}
		
		for(int i=0;i<helpArr.size();i++)
		{
			String userAnswerNumber=(String) helpArr.get(i);
			System.out.println("useranswernumber:"+userAnswerNumber);
			String userAnswerValue=params.get(userAnswerNumber)[0];
			System.out.println("useranswervalue:"+userAnswerValue);
			
			for(int j=0;j<allQuestionList.size();j++)
			{
				TbQuestionbank bank=allQuestionList.get(j);
				if((bank.getTitleId()+"").equals(userAnswerNumber)&&bank.getQuestionRightAnswer().equals(userAnswerValue))
				{
					judge[j]=true;
				}
			}
			
		}
		
		/**
		 * 存放错题详情的集合
		 */
		List<TbQuestionbank> list=new ArrayList();
		/**
		 * 得到错题详情
		 */
		for(int i=0;i<judge.length;i++){
		if(!judge[i])
			{
				list.add(titleDao.getTitleById(theRightList.get(i)));
			}
			
		}

		return list;
	}

	@Override
	public void addScoreAndUserId(TbPaper paper) {
		/**
		 * 获取用户的ID以及PaperId
		 */
		// TODO Auto-generated method stub
		paperDao.addScoreAndUserId(paper);
	}

	@Override
	public List<String> getPaperType() {
		// TODO Auto-generated method stub
		return paperDao.getPaperType();
	}

}